/**
 * 图像渲染策略
 */
var	LBRenderImage	= function (options) {

	if ('undefined' == typeof options.src || '' == typeof options.src) {
		
		throw	"image src can not blank";
	}

	this.setConfig(options);
}

LBRenderImage.prototype.setConfig	= function (options) {

	if ('string' == typeof options.src) {
		
		this._src			= options.src;
	}

	if ('number' == typeof options.sourceX) {

		this._sourceX		= options.sourceX;
	}
	
	if ('undefined' == typeof this._sourceX) {
		
		this._sourceX		= 0;
	}
	
	if ('number' == typeof options.sourceY) {
		
		this._sourceY		= options.sourceY;
	}
	
	if ('undefined' == typeof this._sourceY) {
		
		this._sourceY		= 0;
	}
	
	if ('number' == typeof options.sourceWidth) {
		
		this._sourceWidth	= options.sourceWidth;
	}
	
	if ('undefined' == typeof this._sourceWidth) {
		
		this._sourceY		= 0;
	}
	
	if ('number' == typeof options.sourceHeight) {
		
		this._sourceHeight	= options.sourceHeight;
	}
	
	if ('undefined' == typeof this._sourceHeight) {
		
		this._sourceHeight	= 0;
	}

	if ('number' == typeof options.psitionX) {
		
		this._positionX		=  options.psitionX;
	}
	
	if ('undefined' == typeof this._positionX) {
		
		this._positionX	= 0;
	}
	
	if ('number' == typeof options.psitionY) {
		
		this._positionY		= options.psitionY;
	}
	
	if ('undefined' == typeof this._positionY) {
		
		this._positionY	= 0;
	}
	
	if ('number' == typeof options.displayWidth) {
		
		this._displayWidth	= options.displayWidth;
	}
	
	if ('undefined' == typeof this._displayWidth) {
		
		this._displayWidth	= 0;
	}
	
	if ('number' == typeof options.displayHeight) {
		
		this._displayHeight	= options.displayHeight;
	}
	
	if ('undefined' == typeof this._displayHeight) {
		
		this._displayHeight	= 0;
	}
}

LBRenderImage.imagePool		= {};
LBRenderImage.imageStatPool	= {};

LBRenderImage.prototype.setContext	= function (context) {

	this._context	= context;
}

LBRenderImage.prototype.setPosition	= function (x, y, positionType) {

	this._positionX	= 'undefined' == typeof this._positionX	? 0	: this._positionX;
	this._positionY	= 'undefined' == typeof this._positionY	? 0	: this._positionY;

	if ('absolute' == positionType) {
		
		this._positionX	= x;
		this._positionY	= y;
	} else {
		
		this._positionX	+= x;
		this._positionY	+= y;
	}
}

/**
 * 预加载
 */
LBRenderImage.preload		= function (srcList, callback) {
	
	var	totalImage		= srcList.length;
	var	currentImage	= 0;
	
	for (var offset = 0;offset < totalImage;offst ++) {
		
		var	src								= srcList[offset];
		
		if ('undefined' == typeof LBRenderImage.imageStatPool[src]) {
			
			LBRenderImage.imageStatPool[src]	= false;
			LBRenderImage.imagePool[src]		= new Image;
			LBRenderImage.imagePool[src].addEventListener('load', function (event) {
				
				LBRenderImage.imageStatPool[src]	= true;
				++ currentImage;
				
				if (currentImage == totalImage && 'function' == typeof callback) {
	
					callback();
				}
			});
			LBRenderImage.imagePool[src].src	= src;
		}
	}
}

LBRenderImage.prototype._load	= function (callback) {

	var	src	= this.getSrc();
	var	me	= this;

	if ('undefined' == typeof LBRenderImage.imageStatPool[src]) {

		LBRenderImage.imageStatPool[src]	= false;
		LBRenderImage.imagePool[src]		= new Image;
		LBRenderImage.imagePool[src].addEventListener('load', function (event) {

			LBRenderImage.imageStatPool[src]	= true;
			
			if ('function' == typeof callback) {
				
				callback(event);
			}
		});
		LBRenderImage.imagePool[src].src	= src;

		return	;
	}

	if (true == LBRenderImage.imageStatPool[src] && 'function' == typeof callback) {

		return	callback();
	}
	
	if ('function' == typeof callback) {

		LBRenderImage.imagePool[src].addEventListener('load', callback);
	}
}

LBRenderImage.prototype.getSrc	= function () {
	
	return	this._src;
}

LBRenderImage.prototype.getImage	= function () {
	
	return	LBRenderImage.imagePool[this.getSrc()];
}

LBRenderImage.prototype.draw	= function () {
	
	var	me	= this;
	this._load(function (event) {

		var	src	= me.getSrc();
		var	sx	= me._sourceX;
		var	sy	= me._sourceY;
		var	sw	= me._sourceWidth;
		sw		= sw >= 0	? sw	: me.getImage().width;
		var	sh	= me._sourceHeight;
		sh		= sh >= 0	? sh	: me.getImage().height;
		var	dx	= me._positionX;
		var	dy	= me._positionY;
		var	dw	= me._displayWidth;
		dw		= dw >= 0	? dw	: sw;
		var	dh	= me._displayHeight;
		dh		= dh >= 0	? dh	: sh;
		me._context.drawImage(me.getImage(), sx, sy, sw, sh, dx, dy, dw, dh);
	});
}
